package controller.admin;
import java.io.File;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;

import controller.admin.base.BaseAdminController;
import enums.ServicerStatus;
import model.Servicer;
import service.ServicerService;

public class AdminServicerController extends BaseAdminController {
	@Inject ServicerService servicerService ;

	public void list() {
	    Kv kv = getParamKv() ;
	    Integer pageNumber = getParaToInt("pageNumber", 1);
	    Integer pageSize = getParaToInt("pageSize", 10);
	    set("page", servicerService.getPage( kv , pageNumber, pageSize));
	    set("servicerStatusList", ServicerStatus.toList());
	    renderJson();
	}

	public void delete() {
        Long id = getParaToLong("id");
        Ret ret = servicerService.delete(id);
        renderJson(ret);
    }

	public void info() {
		Long id = getParaToLong("id");
        set("data", Servicer.dao.findById(id) );
        renderJson();
	}

	public void save() {
		Servicer servicer = getModel(Servicer.class , "x");
        Ret ret = servicerService.save(servicer);
        renderJson(ret);
	}
	
	public void audit() {
		boolean isPass = getParaToBoolean("isPass", true);
		Long id = getParaToLong("id");
		String reason = getPara("reason");
		renderJson( servicerService.audit(isPass , id , reason) );
	}
	
	public void export() {
		Kv kv = getParamKv() ;
		File file = servicerService.exportExcel(kv);
    	renderJson("fileName" , file.getAbsolutePath() );
	}
	
	public void imports() {
		try {
			UploadFile file = getFile("upfile");
	    	renderJson(servicerService.importExcel(file.getFile()));
		} catch (Exception e) {
			e.printStackTrace();
			renderFail();
		}
	}
}
